// pages/reward/position/details/details.js
import { $wuxDialog } from '../../../wux-components/index'
import { collect, isCollect} from '../../../utils/collect.js'
const config = getApp().globalData.config
const T = getApp().globalData.T
let token = '';
let userId = -1;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: -1,
    collectStatus: 0,
    isShare: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.data.id = Number(options.id);

    this.setData({ isShare: options.userId });
    // 通过分享进入，且无 token
    if (options.userId && !getApp().globalData.token) {
      const params = {
        userId: options.userId,
        userType: 1
      }
      wx.redirectTo({
        url: '/pages/login/index?' + T.param(params)
      });
    }

    if (options.origin == 'recommend') {
      this.setData({ origin: 'recommend' });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    token = getApp().globalData.token;
    userId = getApp().globalData.userId;
    this.getDetail();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  onCollectPos() {
    let _param = {
      token: token,
      userId: userId,
      type: 0,
      relatedId: this.data.id
    }
    collect(_param).then((res) => {
      T.toast({
        id: '#toast',
        text: this.data.collectStatus ? '取消成功' : '收藏成功'
      })
      this.setData({
        collectStatus: 1 ^ this.data.collectStatus
      })
    })
  },

  getDetail() {
    T.req({
      loadTitle: '加载中...',
      url: config.api + 'reward/position_list/detail',
      method: 'GET',
      data: {
        token: token,
        rewardId: this.data.id
      },
      success: (res) => {
        this.setData({
          obj: res,
          collectStatus: res.collectStatus
        })
      }
    })
  },

  /**
   * 投递简历
   */
  onPublishResume() {
    let param = {
      token: token,
      positionId: this.data.id
    }
      T.req({
        loadTitle: '请求中...',
        url: config.api + 'resume_deliver/resume?' + T.param(param),
        method: 'POST',
        success: (res) => {
          T.toast({
            id: '#toast', text: '投递成功', success: () => {
              this.setData({ isDelive: true });
            }
          });
        }
      })
  },

  /**
   * 我要推荐 - 职位
   */
  onRecommend(e) {
    $wuxDialog().confirm({
      resetOnClose: true,
      closable: true,
      title: '提示',
      content: '内容--todo',
      onConfirm: (res) => {
        const param = {
          id: this.data.companyId, // todo 检测详情接口是否返回公司id
          reAvator: encodeURIComponent(getApp().globalData.userInfo.avator),
          reName: getApp().globalData.userInfo.name,
          position: this.data.obj.name,
          salary: this.data.obj.salaryRange,
          work: this.data.obj.candidateRequire,
          edu: e.currentTarget.dataset.edu,
        };

        // 调接口给添加推荐记录
        const paramRecommend = {
          token: token,
          userId: userId,
          userType: 1,  // 0-应聘用户 1-招聘用户
        };
        T.req({
          loadTitle: '加载中...',
          url: config.api + 'recommendation/position/' + this.data.id + '?' + T.param(paramRecommend),
          method: 'POST',
          data: {},
          success: (res) => {
            param.rewardId = this.data.id;  // 悬赏 id
            param.recommendClassifyId = res.recommendClassifyId;  // 推荐编号
            param.url = 'pages/reward/position/position';

            wx.navigateTo({
              url: '/pages/poster/position/position?' + T.param(param),
            })
          }
        })
      },
      onCancel(e) {

      },
    })
  },

  /**
   * 用户点击分享
   */
  onShareAppMessage: function (res) {
    const params = {
      id: this.data.id,
      userId: userId,
      userType: 1
    }
    return {
      title: this.data.obj.name,
      path: T.getCurrentPageUrl(false) + '?' + T.param(params),
      imageUrl: ''
    }
  }
})